import { storeToRefs } from 'pinia';
import { ElMessage } from 'element-plus';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn'
dayjs.extend(relativeTime)
dayjs.locale('zh-cn')

export class Utils {

   // 验证是否登录: 'pc' || 'h5' , 不同端，弹出登录页不同
   static verifylogin(is: string = 'pc') {
      const TOKEN = useCookie("CNYZSTOKEN", { sameSite: "strict" }) || ''
      if (!TOKEN.value) {
         console.log("未登录");
         if (is === 'pc') {
            const { openLogin } = storeToRefs(cnyzsCommonpinia())
            openLogin.value = true
            ElMessage.warning('请先登录')
            return
         }
         if (is === 'h5') {
            ElMessage.warning('请先登录')
            return
         }
      }
   }

   // 时间处理
   static timeDisplay(time: number): string {
      return dayjs(time).fromNow()
   }

   // 验证是否是今天
   // 参数：时间戳
   static isToday(timestamp: any) {
      const todayTimestamp = Date.now(); // 获取今天的时间戳
      const date = new Date(timestamp); // 将传入的时间戳转换成日期对象
      const today = new Date(todayTimestamp); // 将今天的时间戳转换成日期对象
      const year = date.getFullYear();
      const month = date.getMonth();
      const day = date.getDate();
      const todayYear = today.getFullYear();
      const todayMonth = today.getMonth();
      const todayDay = today.getDate();
      return year === todayYear && month === todayMonth && day === todayDay;
   }

   // 压缩图片
   // 参数：file
   // return { code, fileName, msg, type }
   static compressImages(file: File) {

   }

   // 终端相关：验证是否是终端管理员
   static verifyTerminalAdmin(userinfo: any): boolean {
      if (userinfo?.userpower == 0) return true
      return false
   }

   // 系统相关：验证是否是系统管理员或者是创建者
   // 参数：系统信息，用户信息
   // 返回布尔值
   static verifySystemAdmin(systeminfo: any, userinfo: any): boolean {
      if (systeminfo.systemCreateUser._id === userinfo._id) {
         return true
      } else {
         return systeminfo.systemAuth.some((v: any) => v._id === userinfo._id)
      }
   }

   // 系统相关：进入社区/系统
   // 参数：系统标识
   static gosystem(systemSign: string) {
      navigateTo('/System/' + systemSign)
   }

   // 验证是否登录
   static islogin() {
      const CNYZSTOKEN = useCookie("CNYZSTOKEN", { sameSite: "strict" }) || ''
      if (!CNYZSTOKEN.value) {
         const { openLogin } = storeToRefs(cnyzsCommonpinia())
         openLogin.value = true
         navigateTo('/')
         return
      }
   }

   // 帖子相关：查看帖子/查看原文
   static viewPost(item: any) {
      window.open(item.postsId, '_blank')
   }

   // 帖子相关：查看作者
   static viewAuthor(id: number | string) {
      window.open('/Author/' + String(id), '_blank')
   }

   // 帖子相关：拉黑名单
   static blacklist(item: any) {
      ElMessage.error('开发中')
   }

   // 帖子相关：举报内容 
   static reportContent(item: any) {
      ElMessage.error('开发中')
   }

   // 帖子相关：全局置顶
   static async globalTopping(item: any) {
      const res: any = await cnyzsRequest('/api/cnyzs-system-posts/Front-Global-Top', {
         method: 'POST',
         data: {
            systemId: item.postsSystemId, // 系统ID
            postsId: item._id, // 帖子_ID
         }
      })
      if (res.status !== 200) return ElMessage.warning(res.message)
      ElMessage.success(res.message)
   }

   // 帖子相关：置顶帖子 (当前系统置顶)
   // 参数：整条帖子数据 item
   static async contentTopping(item: any) {
      const res: any = await cnyzsRequest('/api/cnyzs-system-posts/Front-Posts-Top', {
         method: 'POST',
         data: {
            systemId: item.postsSystemId, // 系统ID
            postsId: item._id, // 帖子_ID
         }
      })
      if (res.status !== 200) return ElMessage.warning(res.message)
      ElMessage.success(res.message)
   }

   // 帖子相关：推荐帖子
   static async recommendedContent(item: any) {
      const res: any = await cnyzsRequest('/api/cnyzs-system-posts/Front-Posts-Recommend', {
         method: 'POST',
         data: {
            systemId: item.postsSystemId, // 系统ID
            postsId: item._id, // 帖子_ID
         }
      })
      if (res.status !== 200) return ElMessage.warning(res.message)
      ElMessage.success(res.message)
   }

   // 评论相关：置顶评论
   // 参数：整条评论数据 item
   static async commentTop(item: any) {
      const res: any = await cnyzsRequest('/api/cnyzs-system-posts-comment/top', {
         method: 'POST',
         data: {
            systemId: item.commentSystemId,
            postsId: item.commentSystemPostsId,
            commentId: item._id
         }
      })
      ElMessage.success(res.message)
   }

}